from receiver.topic_handler import TopicHandler
from px4.drone_controller import DroneController

droneController = DroneController()

class BasicAircraftTopicHandler(TopicHandler):
    def __init__(self):
        super().__init__()

    def enable_handle(self, topic: str) -> bool:
        return "/service/basicAircraftService" in topic

    async def handle(self, message: dict) -> int:
        try:
            self.bid = self.get_bid(message)
            self.method = self.get_method(message)
            if self.method == "take_off":
                await droneController.arm_and_takeoff()
            elif self.method == "land":
                await droneController.land()
            elif self.method == "return_to_launch":
                await droneController.return_to_launch()
            elif self.method == "start_task":
                data = self.get_data(message)
                task_execute_id = data["taskExecuteId"]
                droneController.set_task_execute_id(task_execute_id)
            elif self.method == "stop_task":
                droneController.set_task_execute_id(0)
            else:
                print(f"Unknown method: {self.method}")
                return 1
        except RuntimeError as e:
            print(f"Error handling message: {str(e)}")
            return 1